package com.beta.auth.common;

import com.alibaba.fastjson.annotation.JSONField;
import com.beta.user.auth.center.api.response.CommonResultEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author by lchen2
 * @Classname ResultVo
 * @Date 2021/7/1 3:10 下午
 */
@Api("通用返回")
@Data
public class ResultVo<T> {

    @JSONField(name = "ErrorMessage")
    @ApiModelProperty("错误消息")
    private String ErrorMessage;

    @JSONField(name = "ErrorCode")
    @ApiModelProperty("错误码")
    private Integer ErrorCode;

    @JSONField(name = "State")
    @ApiModelProperty("返回码 0 成功 其他失败")
    private Integer State;

    @JSONField(name = "Data")
    @ApiModelProperty("返回数据")
    private T Data;

    public ResultVo() {
        this.setState(0);
        this.setErrorCode(0);
        this.setErrorMessage("请求成功！");
    }

    public static <T> ResultVo<T> success() {
        return new ResultVo<>();
    }

    public static <T> ResultVo<T> success(T data){
        ResultVo<T> resultVo = new ResultVo<>();
        resultVo.setData(data);
        return resultVo;
    }

    public static <T> ResultVo<T> success(T data,Integer total){
        ResultVo<T> resultVo = new ResultVo<>();
        resultVo.setData(data);
        return resultVo;
    }

    public static <T> ResultVo<T> failure(CommonResultEnum commonResultEnum) {
        ResultVo<T> resultVO = new ResultVo<>();
        resultVO.setState(0);
        resultVO.setErrorCode(commonResultEnum.getState());
        resultVO.setErrorMessage(commonResultEnum.getMessage());
        return resultVO;
    }

    public static <T> ResultVo<T> failure(Integer code, String msg) {
        ResultVo<T> resultVO = new ResultVo<>();
        resultVO.setState(0);
        resultVO.setErrorCode(code);
        resultVO.setErrorMessage(msg);
        return resultVO;
    }

    public static <T> ResultVo<T> failure(Integer code, String msg, T result) {
        ResultVo<T> resultVO = new ResultVo<>();
        resultVO.setState(0);
        resultVO.setErrorMessage(msg);
        resultVO.setErrorCode(code);
        resultVO.setData(result);
        return resultVO;
    }

}
